package util

import (
	"net/http"
	"net/url"
)

const (
	HN_CONNECTION   = "Connection"
	HN_CONTENT_TYPE = "Content-Type"
)

func SetStatuCode(w http.ResponseWriter, code int) {
	w.WriteHeader(code)
}

func SetContentTypeText(h http.Header) {
	h.Set(HN_CONTENT_TYPE, "text/plain; charset=utf-8")
}

func SetContentTypeJson(h http.Header) {
	h.Set(HN_CONTENT_TYPE, "application/json; charset=utf-8")
}

func SetKeepAlive(h http.Header, keepAlive bool) {
	if keepAlive {
		h.Del(HN_CONNECTION)
	} else {
		h.Set(HN_CONNECTION, "close")
	}
}

func CloneUrl(s *url.URL) url.URL {
	return url.URL{
		Scheme:      s.Scheme,
		Opaque:      s.Opaque,
		User:        s.User,
		Host:        s.Host,
		Path:        s.Path,
		RawPath:     s.RawPath,
		ForceQuery:  s.ForceQuery,
		RawQuery:    s.RawQuery,
		Fragment:    s.Fragment,
		RawFragment: s.RawFragment,
	}
}
